-- NewTeamMemberView

local NewTeamMemberPanel = import(".NewTeamMemberPanel")

local disOFNameAndBg = 0 -- 目标名字与背景图的X差距
local targetnameBgContentSize = nil -- 目标初始底尺寸

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.NewTeamMemberUi

-- 将需要操作的UI控件对应到成员
-- 以后改UI控件名字或者层级的时候只需要改此处对应关系即可
function ClassRef:bindUINameToLocalName()
	self._closeBtn = self._ui["Panel/Button_close"]
	self._relocateBtn = self._ui["Panel/Panel_top/Button_3"]

	-- 队伍成员node
	self._teamMemberNode = self._ui["Panel/team"]
	local teamMemberPanel = self._teamMemberNode:getChildByName( "Panel" )
	teamMemberPanel:getChildByName( "Panel_tlt_team" ):setVisible( true )
	teamMemberPanel:getChildByName( "Panel_bom_1" ):setVisible( true )
	teamMemberPanel:getChildByName( "Panel_bom_2" ):setVisible( true )
	teamMemberPanel:getChildByName( "Panel_tlt_fb" ):setVisible( false ) -- 进入战斗之前的准备UI用，此View一定不显示
	-- 成员列表
	self._teamMemberInfo = NewTeamMemberPanel.create( teamMemberPanel:getChildByName( "player" ) )
	self._teamMemberInfo:registerOnOperate(function(op, ...)
				executeFunc(self._onOperate, op, ...) -- 交由controller处理
			end)

	-- 战斗力
	self._panelBom2 = teamMemberPanel:getChildByName("Panel_bom_2")
	self._compatPowerImg = self._panelBom2:getChildByName("img")
	self._compatPowerTextTlt = self._panelBom2:getChildByName( "combat_tlt" )
	self._compatPowerText = self._panelBom2:getChildByName( "combat_num" )

	self._panelBom1 = teamMemberPanel:getChildByName("Panel_bom_1")
	-- 退出队伍按钮
	self._quitTeamBtn = teamMemberPanel:findChild( "Panel_bom_1/Button_leave" )
	-- 申请按钮
	self._shownApplyBtn = teamMemberPanel:findChild( "Panel_bom_1/Button_apply" )
	self._applyBtnRedPoint = self._shownApplyBtn:getChildByName( "dotNew" )
	-- 升级成军团按钮
	self._upToLegionBtn = teamMemberPanel:findChild( "Panel_bom_1/Button_legion" )
	-- 降级为队伍按钮
	self._downToTeamBtn = teamMemberPanel:findChild( "Panel_bom_1/Button_team" )
	-- 队伍设置按钮
	self._settingBtn = teamMemberPanel:findChild( "Panel_bom_1/Button_set" )

	-- 队伍目标
	self._target = self._ui["Panel/Panel_top/target"]

	-- 自动跟随节点
	self._autoFollowNode = self._ui["Panel/Panel_top/Node_autoFollow"]
	self._autoFollowSelectedBtn = self._ui["Panel/Panel_top/Node_autoFollow/selected_btn"]

	-- 召集
	self._inviteFollowBtn = self._ui["Panel/Panel_top/btn_leader"]
	print("self._inviteFollowBtn", self._inviteFollowBtn)

	self._targetBg = self._ui["Panel/Panel_bg/bg04"]
	targetnameBgContentSize = self._targetBg:getContentSize()
	disOFNameAndBg = self._target:getPositionX() - self._targetBg:getPositionX()
end

function ClassRef:init()
	self.isAutoFollowFlag = false -- 是否跟随标记

	self:bindUINameToLocalName()

	-- 关闭UI
	self._closeBtn:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)
	-- 重定位到地下城队伍搜索器
	self._relocateBtn:addClickEventListener(function()
		executeFunc(self._onOperate, "TeamMember_relocateToTeamSearching")
	end)
	-- 切换自动跟随状态
	self._autoFollowSelectedBtn:addClickEventListener(function()
		if not self.isAutoFollowFlag then -- 自动跟随
			local creataeTeamDialog = display.newConfirmDialog( L("lua_code_text_429"),
				function(sender) -- 确定
					executeFunc(self._onOperate, "TeamMember_changeAutoFollowStatus")
					if cc.isAlive(self) then
						self:setAutoFollowStatus( not self.isAutoFollowFlag )
					end
				end)
			UIHandler:addPopDialog( creataeTeamDialog )
		else
			executeFunc(self._onOperate, "TeamMember_changeAutoFollowStatus")
			self:setAutoFollowStatus( not self.isAutoFollowFlag )
		end
	end)

	-- 召集队友
	self._inviteFollowBtn:addClickEventListener(function()
		executeFunc(self._onOperate, "TeamMember_inviteFollow")
	end)

	-- 退出队伍
	self._quitTeamBtn:addClickEventListener(function()
		local creataeTeamDialog = display.newConfirmDialog( L("lua_code_text_51"),
			function() -- 确定
				executeFunc(self._onOperate, "TeamMember_quitTeam")
			end)
		UIHandler:addPopDialog( creataeTeamDialog )
	end)

	-- 弹出申请列表
	self._shownApplyBtn:addClickEventListener(function()
		executeFunc(self._onOperate, "TeamMember_showApplyList")
		self:refreshNewApplyerFlag( false )
	end)

	-- 队伍升级成为军团
	self._upToLegionBtn:addClickEventListener(function()
		local creataeTeamDialog = display.newConfirmDialog( L("lua_code_text_33"),
			function() -- 确定
				executeFunc(self._onOperate, "TeamMember_changeTeamGrade")
			end)
		UIHandler:addPopDialog( creataeTeamDialog )
	end)

	-- 军团降级为队伍
	self._downToTeamBtn:addClickEventListener(function()
		executeFunc(self._onOperate, "TeamMember_changeTeamGrade")
	end)

	self._settingBtn:addClickEventListener(function()
		executeFunc(self._onOperate, "TeamMember_showTeamSettingUI")
	end)

	if DungeonManager:isSecret() then
		self:addTouchMask()
	end

	-- 跨服副本中队伍操作按钮不可见
	local isCrossDungeon = DungeonManager:isCrossDungeon()
	self._panelBom1:setVisible(not isCrossDungeon)
	self._autoFollowNode:setVisible(not isCrossDungeon)
end

function ClassRef:addTouchMask()
	local l = ccui.Layout:create()
	-- l:setBackGroundColor(display.COLOR_GRAY)
	-- l:setBackGroundColorType(1)
	l:setTouchEnabled(true)

	l:setContentSize(self._root:findChild("Panel/team"):findChild("Panel"):getContentSize())
	l:setPosition(self._root:findChild("Panel/team"):getPosition())
	l:addClickEventListener(function()
		display.pushToast(L("scetert_teamforbidden"))	
	end)
	self._root:addChild(l)
end

function ClassRef:show()
	self:setVisible(true)
	self:openAction()
end

function ClassRef:refreshTeamMember( newTeamVo )
	self._teamMemberInfo:setData( newTeamVo )
end

-- 刷新队长随从信息
function ClassRef:refreshPartnerInfo( maxNums, partnerInfo )
	self._teamMemberInfo:refreshPartnerInfo( maxNums, partnerInfo )
end

-- 刷新 队长/队员 可操作项的显示状态
function ClassRef:btnShownAccordingToIsLeader( isleader )
	if isleader then -- 队长？
		self._shownApplyBtn:setVisible( true )
		self._upToLegionBtn:setVisible( TEAM_LEGION_ENABLE and true )
		self._downToTeamBtn:setVisible( TEAM_LEGION_ENABLE and true )
	else
		self._shownApplyBtn:setVisible( false )
		self._upToLegionBtn:setVisible( false )
		self._downToTeamBtn:setVisible( false )
	end
end

-- 刷新跟随／召唤可见性
function ClassRef:refreshTeamFollow(team)
	if team then
		if DungeonManager:isCrossDungeon() then
			self._autoFollowNode:setVisible(false)
		else
			local isMyTeam = team:isMyTeam()
			local isAllWithTeam = team:isAllWithTeam()
			self._autoFollowNode:setVisible( not isMyTeam )
			self._inviteFollowBtn:setVisible( isMyTeam and not isAllWithTeam )
		end
	end
end

-- 自动跟随按钮状态
function ClassRef:setAutoFollowStatus( isAutoFollow )
	self.isAutoFollowFlag = isAutoFollow
	self._autoFollowSelectedBtn:setBright( isAutoFollow )
end

-- 自动跟随按钮可点击态
function ClassRef:setAutoFollowBtnClickEnabled( clickEnable )
	display.setBtnTouchEnable( self._autoFollowSelectedBtn, clickEnable )
end

-- 设置队伍目标
function ClassRef:setTarget( target )
	if "" == target then
		self._target:setString( L("lua_code_text_55") )
	else
		self._target:setString( target )
	end
	self._targetBg:setContentSize( cc.size( disOFNameAndBg + self._target:getVirtualRendererSize().width + 80, targetnameBgContentSize.height ) )
end

-- 刷新队伍战斗力
function ClassRef:refreshTeamCombatPower( combatPower )
	self._compatPowerText:setString( combatPower )
	-- 设置居中对齐
	display.setNodesMiddle(self._panelBom2, self._compatPowerImg, self._compatPowerTextTlt, self._compatPowerText)
end

-- 根据 TeamGrade 刷新 队伍/军团 按钮显示状态
function ClassRef:refreshBtnShownWithTeamGrade( isleader, teamGrade )
	if isleader then
		self._upToLegionBtn:setVisible( TEAM_LEGION_ENABLE and 0 == teamGrade )
		self._downToTeamBtn:setVisible( TEAM_LEGION_ENABLE and 1 == teamGrade )
	else
		self._upToLegionBtn:setVisible( false )
		self._downToTeamBtn:setVisible( false )
	end
end

-- 刷新新申请者new标记状态
function ClassRef:refreshNewApplyerFlag( visible )
	self._applyBtnRedPoint:setVisible( visible )
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

return ClassRef
